package com.chucang.shucang.bac.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chucang.shucang.bac.entity.ManagerEntity;
import com.chucang.shucang.bac.entity.ManagerRoleEntity;
import com.chucang.shucang.bac.entity.RoleEntity;
import com.chucang.shucang.bac.service.ManagerRoleService;
import com.chucang.shucang.bac.service.ManagerService;
import com.chucang.shucang.bac.service.RoleService;
import com.chucang.shucang.bac.vo.BacUserInfoVO;
import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 管理员表
 *
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-09-11
 */
@RestController
@RequestMapping("/manager")
@RequiredArgsConstructor
@Api(tags = "管理员表")
public class ManagerController {

    private final ManagerService managerService;
    private final ManagerRoleService managerRoleService;
    private final RoleService roleService;

    @Inner
    @GetMapping("/getManagerInfoByUsername/{username}")
    public R<BacUserInfoVO> getManagerInfoByUsername(@PathVariable String username) {
        LambdaQueryWrapper<ManagerEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerEntity::getUsername, username)
                .select();
        ManagerEntity one = managerService.getOne(lambdaQueryWrapper);
        BacUserInfoVO bacUserInfoVO = new BacUserInfoVO();
        if (Objects.nonNull(one)) {
            BeanUtil.copyProperties(one, bacUserInfoVO);
        }
        //关联角色查询
        LambdaQueryWrapper<ManagerRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ManagerRoleEntity::getManagerId, one.getId())
                .select();
        List<ManagerRoleEntity> list = managerRoleService.list(queryWrapper);
        List<Integer> roleList = new ArrayList<>(8);
        if (!CollUtil.isEmpty(list)) {
            list.forEach(c -> roleList.add(c.getRoleId()));
        }
        LambdaQueryWrapper<RoleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(RoleEntity::getId, roleList)
                .select(RoleEntity::getTitle, RoleEntity::getMenuCode);
        List<RoleEntity> roleEntities = roleService.list(wrapper);
        if (!CollUtil.isEmpty(roleEntities)) {
            List<String> menuCodes = new ArrayList<>(8);
            List<String> roleTitles = new ArrayList<>(8);
            roleEntities.forEach(r -> {
                menuCodes.add(r.getMenuCode().toString());
                roleTitles.add(r.getTitle());
            });
            bacUserInfoVO.setRoles(roleTitles);
            bacUserInfoVO.setMenuCodes(menuCodes);
        }
        return R.ok(bacUserInfoVO);
    }
}